//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SecondDerivativeOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Operators/TripleBandLinearOp.h>
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (UInt64 direction, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher) : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phSecondDerivativeOp = NULL;
#endif
        QuantLib::Size _direction = (QuantLib::Size)ValueHelper::Convert (direction); //d
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppSecondDerivativeOp = new boost::shared_ptr<QuantLib::SecondDerivativeOp> (new QuantLib::SecondDerivativeOp ( _direction,  _mesher ));
        SetTripleBandLinearOp (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (boost::shared_ptr<QuantLib::SecondDerivativeOp>& childNative, Object^ owner) : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondDerivativeOp = NULL;
#endif
	_ppSecondDerivativeOp = &childNative;
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (QuantLib::SecondDerivativeOp& childNative, Object^ owner) : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondDerivativeOp = NULL;
#endif
	_ppSecondDerivativeOp = new boost::shared_ptr<QuantLib::SecondDerivativeOp> (&childNative);
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
    _SecondDerivativeOpOwner = owner;
    _TripleBandLinearOpOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (CSecondDerivativeOp^ copy) : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondDerivativeOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSecondDerivativeOp = new boost::shared_ptr<QuantLib::SecondDerivativeOp> (copy->GetShared());
        _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (PLATFORM::Type^ t) : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondDerivativeOp = NULL;
#endif
	if (!t->IsSubclassOf(CSecondDerivativeOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (QuantLib::Handle<QuantLib::SecondDerivativeOp>& childNative, Object^ owner)  : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
	_phSecondDerivativeOp = &childNative;
	_ppSecondDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::SecondDerivativeOp>>(childNative.currentLink());
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
    _SecondDerivativeOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (QuantLib::Handle<QuantLib::SecondDerivativeOp> childNative)  : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
	_phSecondDerivativeOp = &childNative;
	_ppSecondDerivativeOp = &static_cast<boost::shared_ptr<QuantLib::SecondDerivativeOp>>(childNative.currentLink());
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::CSecondDerivativeOp (QuantLib::SecondDerivativeOp childNative)  : CTripleBandLinearOp(CSecondDerivativeOp::typeid)
{
#ifdef HANDLE
	_phSecondDerivativeOp = NULL;
#endif
	_ppSecondDerivativeOp = new boost::shared_ptr<QuantLib::SecondDerivativeOp> (new QuantLib::SecondDerivativeOp (childNative));
    _ppTripleBandLinearOp = new boost::shared_ptr<QuantLib::TripleBandLinearOp> (boost::dynamic_pointer_cast<QuantLib::TripleBandLinearOp> (*_ppSecondDerivativeOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::~CSecondDerivativeOp ()
{
    if (_ppSecondDerivativeOp != NULL)
    {
	    delete _ppSecondDerivativeOp;
        _ppSecondDerivativeOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::!CSecondDerivativeOp ()
{
    if (_ppSecondDerivativeOp != NULL)
    {
	    delete _ppSecondDerivativeOp;
    }
}
QuantLib::SecondDerivativeOp& Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::GetReference ()
{
    if (_ppSecondDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return **_ppSecondDerivativeOp;
}
boost::shared_ptr<QuantLib::SecondDerivativeOp>& Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::GetShared ()
{
    if (_ppSecondDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return *_ppSecondDerivativeOp;
}
QuantLib::SecondDerivativeOp* Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::GetPointer ()
{
    if (_ppSecondDerivativeOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppSecondDerivativeOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SecondDerivativeOp>& Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::GetHandle ()
{
	if (_phSecondDerivativeOp == NULL)
	{
		_phSecondDerivativeOp = new Handle<QuantLib::SecondDerivativeOp> (*_ppSecondDerivativeOp);
	}
	return *_phSecondDerivativeOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp::HasNative () 
{
	return (_ppSecondDerivativeOp != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::ISecondDerivativeOp^ Cephei::QL::Methods::Finitedifferences::Operators::CSecondDerivativeOp_Factory::Create (UInt64 direction, Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher)
{
    return REFNEW CSecondDerivativeOp ( direction,  mesher);
}
